<?php

namespace Wonfu\Core\Database;

use DI\Annotation\Inject;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManager;
use Nette\PhpGenerator\PhpFile;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function Symfony\Component\String\u;

/**
 * Description of KoCommand
 *
 * @author vison.cao <vison.cao@huolala.cn>
 */
class CreateModelCommand extends Command
{

    // the name of the command (the part after "bin/console")
    protected static $defaultName = 'app:create-model';

    /**
     * @var EntityManager
     */
    private $em;

    /**
     * @Inject()
     * @param EntityManager $em
     */
    public function setEm(EntityManager $em): void
    {
        $this->em = $em;
    }


    protected function configure()
    {
        $this->setDescription('Creates the table file.')
            ->addArgument('name', true, 'entity name')
            ->addOption('namespace', ['ns'], InputOption::VALUE_REQUIRED, '命名空间', "App\Entity")
            ->addOption('dir', ['d'], InputOption::VALUE_REQUIRED, '目录', "./src/Entity")
            ->addOption('force', ['f'], InputOption::VALUE_OPTIONAL, "是否覆盖", true)
            ->setHelp('This command allows you to create a user...');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        /** @var EntityManager $em */
        $em = $this->em;
        $sm = $em->getConnection()->createSchemaManager();
        $name = $input->getArgument('name');
        $force = $input->getOption('force');
        $dir = $input->getOption('dir');
        $namespace = $input->getOption('namespace');
        $columns = $sm->listTableColumns($input->getArgument('name'));

        $entityName = u($name)->camel()->title();

        $filename = $dir . '/' . $entityName . '.php';
        if (file_exists($filename) && $force) {
            $output->write("file exists", true);
            return Command::FAILURE;
        }
        touch($filename);
        $file = new PhpFile;
        $namespace = $file->addNamespace($namespace);
        $class = $namespace->addClass($entityName);
        $class->setComment('this file auto generate by <code>php bin/console app:create-model</code>');
        foreach ($columns as $key => $value) {
            $class->addProperty(u($value->getName())->camel())
                ->setPrivate()
                ->addComment($value->getComment() . "\n")
                ->addComment('@var ' . $this->toConvertPHPType($value->getType()));
        }
        file_put_contents($filename, $file);
        $output->writeln('success create the entity and table file!');
        return Command::SUCCESS;
    }

    /**
     * @param Type $type
     * @return string
     */
    private function toConvertPHPType(Type $type):string
    {
        if (in_array($type->getName(), [Types::BIGINT, Types::BIGINT, Types::BOOLEAN, Types::SMALLINT])) {
            return Types::INTEGER;
        }
        return Types::STRING;
    }

}
